package cn.afactory.tygz.model;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.common.ErrorCode;

/**
 * Created with Android Studio.
 * Title:NearByUserInfo
 * Description:
 * Copyright:Copyright (c) 2015
 * Company:和平必胜、正义必胜、人民必胜
 * Author:炎黄子孙
 * Date:15/9/14
 * Time:上午10:01
 * Version 1.0
 */
public class NearByUserInfoList extends BaseDataHeader implements Serializable {

    private static final long serialVersionUID = -7794201820930475492L;

    public List<NearByUserInfo> value;

    public int count = 0;
    /**
     * 反序列化函数，用于从json字符串反序列化本类型实例
     */
    public static NearByUserInfoList deserialize(String json) throws JSONException {
        if (json != null && !json.isEmpty()) {
            return deserialize(new JSONObject(json));
        }
        return null;
    }
    /**
     * 反序列化函数，用于从json节点对象反序列化本类型实例
     */
    public static NearByUserInfoList deserialize(JSONObject json) throws JSONException {
        if (json != null && json != JSONObject.NULL && json.length() > 0) {
            NearByUserInfoList result = new NearByUserInfoList();

            String message = json.optString("message");
            int state = json.optInt("state");
            json = json.optJSONObject("data");
            if(state == ErrorCode.STATUS_OK) {
                if(json != null) {
                    result.count = json.optInt("count");
                    JSONArray jsonArray = json.optJSONArray("value");
                    if (jsonArray != null) {
                        int len = jsonArray.length();
                        result.value = new ArrayList<NearByUserInfo>(len);
                        for (int i = 0; i < len; i++) {
                            JSONObject jo = jsonArray.optJSONObject(i);
                            if (jo != null && jo != JSONObject.NULL && jo.length() > 0) {
                                result.value.add(NearByUserInfo.deserialize(jo));
                            }
                        }
                    }
                }
            }

            result.message = message;
            result.state = state;
            return result;
        }
        return null;
    }
}
